package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhangmeng
 * @description 权限控制器
 * @date 2019/9/18
 **/
@RestController
@RequestMapping("/permission")
@Slf4j
public class PermissionController {

    @DubboReference
    private PermissionService permissionService;

    /**
     * 添加
     *
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission) {
        log.info("[权限-添加]data:{},Permission:{}", permission);
        //RPC请求添加
        permissionService.add(permission);
        return new Result(true, MessageConst.ADD_Permission_SUCCESS);
    }


    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @GetMapping("findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        log.info("[权限-分页查询]data:{}", queryPageBean);
        //rpc查询结果
        PageResult pageResult = permissionService.pageQuery(queryPageBean);
        //构造返回对象
        return new Result(true, MessageConst.ACTION_SUCCESS,pageResult);

    }
    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        log.info("[权限-根据id查询]id:{}", id);

        //rpc 调用业务处理
        Permission permission = permissionService.findById(id);
        //封装返回值
        return new Result(true, MessageConst.QUERY_Permission_SUCCESS, permission);

    }

    /**
     * 编辑
     *
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Permission permission) {
        log.info("[权限-编辑]data:{}",permission);

        //RPC请求添加
        permissionService.edit(permission);
        return new Result(true, MessageConst.EDIT_Permission_SUCCESS);

    }


    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        log.info("[权限-根据id删除]id：{}",id);
        //RPC调用处理业务
        permissionService.delete(id);
        return new Result(true,MessageConst.DELETE_Permission_SUCCESS);
    }
}
